package com.mysteel.order.listener;

import com.mysteel.order.entity.OrderEntity;
import com.mysteel.order.exception.CommonException;
import com.mysteel.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * ProjectName: mail
 * ClassName: OrderCloseListener
 * Description:
 * date: 2021/03/31/031 11:32
 *
 * @author xiaochao
 */
@Slf4j
@Service
@RabbitListener(queues = {"order.release.order.queue"})
public class OrderCloseListener {

    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void listenReleaseOrderQueue(Message message, Channel channel, OrderEntity orderEntity){
        try {
            orderService.closeOrder(orderEntity);
            // 避免订单关闭后还能用支付宝支付，手动调用支付宝收单

            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            try {
                channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
            } catch (IOException e1) {
                log.error("消息确认失败，重新入队失败");
            }
        }
    }
}
